package cn.cnlee.app.remote.interceptor;

import java.io.IOException;

import cn.cnlee.arch.util.Logger;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class LoggingInterceptor implements Interceptor {

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request request = chain.request();

        long t1 = System.nanoTime();
        Logger.e(String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.body()));
        Response response = chain.proceed(request);

        long t2 = System.nanoTime();
        Logger.e(String.format("Received response for %s in %.1fms", response.request().url(), (t2 - t1) / 1e6d));
        return response;
    }
}
